<?php

class Brand extends MY_Controller {
    protected $table = 'tbl_brand';
    protected $col_id = 'bra_id';
   
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('brand_model');
        
    }
    public function index()
    {
        
        $data['list_brand'] = $this->brand_model->list_tab($this->table);
        $this->layout('brand/index', $data);
       
    }
    public function set_validate($id)
    {
        if(isset($id) && $id != '')
        {
            $this->form_validation->set_rules('name', 'Brand name', 'required|is_unique[tbl_brand.bra_name.bra_id.'.$id.']');  
            
        }
        else
        {
            
            $this->form_validation->set_rules('name', 'Brand name', 'required|is_unique[tbl_brand.bra_name]');
        } 
       
        $this->form_validation->set_message('required', '%s  is not empty');
        
    }
    public function insert()
    {
        $this->set_validate('');
        $name = $this->input->post('name');
        $data_add = array(
                'bra_name' => $name
            );
        if($this->form_validation->run()=== FALSE)
        {   
            
            $this->layout('brand/insert');
        }
        else
        {
            
            $this->brand_model->action_tab($this->table, $data_add, '');
             redirect('admin/brand/index');   
        }
    }
    public function edit($id = '')
    {
        if(isset($id) && $id != '')
        {
            $this->set_validate($id);
            $name = $this->input->post('name'); 
            $data_edit = array( 'bra_name' => $name);
            
            if($this->form_validation->run()=== FALSE)
            {
                $data['get_one'] = $this->brand_model->get_one($this->table, $this->col_id, $id);
                $this->layout('brand/edit',$data);
            }
            else
            {
                $this->brand_model->action_tab($this->table, $data_edit, $this->col_id, $id);
                redirect('admin/brand/index');        
            }
        }
        
    }
    
    public function delete($id = '')
    {
        if(isset($id) && $id != '')
        {
           $this->brand_model->detele_one($this->table, $this->col_id, $id); 
           redirect('index.php/admin/brand/index'); 
        }
    }
    
    
}

